package business

import enums.Direction
import model.IView

/**
 * 移动行为接口
 */
interface Moveable:IView {
    var badDirection:Direction?//不能移动的方向
    var direction:Direction
    val speed:Int

    /**
     * 将在哪个方向上发生碰撞
     */
    fun willCollision(block:Blockable):Direction?{
        var xx=this.x
        var yy=this.y
        when(this.direction){
            Direction.UP -> yy-=speed
            Direction.DOWN -> yy+=speed
            Direction.LEFT -> xx-=speed
            Direction.RIGHT -> xx+=speed
        }
        var collision=when{
            (xx+this.width)<=block.x ->false
            xx>=(block.x+block.width) -> false
            (yy+this.height)<=block.y ->false
            yy>=(block.y+block.width) -> false
            else ->true
        }
        return if(collision) direction else null
    }

    fun notifyCollision(direction: Direction?,block: Blockable?){
        this.badDirection=direction
    }
}